home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / bastips2.arc / BASMENU.TXT < prev    next >
Encoding:
Text File  |  1988-11-30  |  16.4 KB  |  344 lines

  1.                      BASICally Better Menus
  2.           (PC World October 1986 by Lawrence J. Magid)
  3.  
  4.      DOS is every user's first adversary.  Over time a grudging
  5. fondness develops for this most essential for PC programs.  But even
  6. hardened users will probably admit that there must be a better way to
  7. get at programs and access DOS functions.
  8.      One solution is to buy a front-end menu program.  Installing a
  9. front end conceals the uncommunicative DOS, replacing it with an
  10. informative system that makes formatting a disk, running a program,
  11. or navigating through a thicket of subdirectories a matter of picking
  12. a number off the screen.
  13.      Although the market features a number of these programs, creating
  14. your own is surprisingly easy and gives you complete control over the
  15. look and the scope of the menu.
  16.      With BASIC, you have flexible alternatives.  The first enlists
  17. BASIC 3.0's SHELL command; the second, any version of BASIC combined
  18. with two batch files and a RAM disk.
  19.      BASIC's functions give a menu system the ability to check for
  20. input errors, choose text colors, and branch to submenus that let you
  21. perform mundane but vital tasks such as formatting disk, copying files,
  22. adn backing up hard disks.
  23.      BASIC can also effectively insulate the user from DOS, preventing
  24. inexperienced users from accidentally erasing files or those with
  25. suspect motives from gaining entry to restricted data or progarms.
  26. However, such a system cannot prevent a determined (and sophisticated)
  27. user from breaking and entering -- and wreaking havoc.
  28.      One way to build a menu system with BASIC is to display menu
  29. options with the PRINT command, accept keyboard input via the reserved
  30. variable INKEY$, and execute the appropriate commercial program or DOS
  31. command from within the menu program with BASIC's SHELL command.  The
  32. BASIC interpreter and the menu program remain in memory while the
  33. chosen menu option executes.  Although SHELL can deal with batch files
  34. and programs that have .COM and .EXE file extensions, it cannot run
  35. another interpreted BASIC program or load a memory-resident utility
  36. such as SideKick or ProKey.
  37.      With IBM's BASIC.COM, the short menu program SHELL.BAS takes up
  38. about 21K of memory.  With the BASIC, BASICA, or GWBASIC supplied on
  39. disk with most PC compatibles, the memory overhead can jump to 80K.
  40.      At the beginning of SHELL.BAS, the screen is cleared and set in
  41. 80-column text mode.  On a monitor driven by an IBM Monochrome Display
  42. Adapter, the program prints "BASIC Shell Menu" underlined and skips
  43. two lines.  If the monitor is hooked up to a graphics board, you will
  44. have to replace COLOR 1,0 on line 10 with COLOR 0,7 to display the
  45. title in inverse video.
  46.      Lines 30 through 90 print the menu selections and the prompt
  47. "Enter choice: __" to the screen.  Line 100 sets the variable CHOICE
  48. to 0 and begins a WHILE ...WEND loop that displays the date and time
  49. while resetting the value of CHOICE based on keyboard input.  (The
  50. program considers alphabetic characters, punctuation, and other
  51. symbols to be 0.)  As long as CHOICE is less than 1 or greater than 6
  52. (the number of menu options), the loop continues.
  53.      A valid menu selection ends the loop, and line 150 (PRINT CHOICE)
  54. displays it on screen.  The ON ...GOSUB statement, based on the numeric
  55. value of CHOICE, branches execution to a section of the program.  If
  56. CHOICE=1, the program jumps to line 1000; if CHOICE=2, the subroutine
  57. at line 2000 does the honors.  The ON ...GOSUB construct also
  58. eliminates the need for a cascade of IF statements, but it does
  59. require that menu choices be numeric.
  60.      The SHELL commands starting at lines 1000, 2000, and 3000 load
  61. the programs tied respectively to menu choices 1, 2, and 3.  (The
  62. sequence of SHELL commands on each line is the same as if you'd
  63. entered the individual commands, such as CD\123, at the command line.)
  64. Each swatch of code representing a selection ends with RETURN, which
  65. sends execution back to line 170 and hence to line 10, which displays
  66. the menu.
  67.      To check a disk (menu option 4) the program takes advantage of
  68. BASIC's ability to query the user for additional information.  On line
  69. 4010, INPUT$(1) tells the program to proceed after accepting a single
  70. keystroke.  This tactic eliminates the need for the user to press
  71. <Enter> after typing the disk drive letter, which is then assigned to
  72. the DRIVE$ variable.  The variable CHK$ on line 4020 is assigned to
  73. DOS's CHKDSK command, a space, the drive letter passed to DRIVE$, and
  74. a colon.  SHELL CHK$ passes the entire command (for example, CHKDSK B:)
  75. to DOS.  RETURN then sends control back to line 170 and pops up the
  76. menu again.
  77.      The fifth menu item is processed in much the same manner, although
  78. SHELL.BAS asks for information about the files to be copied.  Menu
  79. option 6 exits to DOS; typing EXIT at the DOS prompt returns the menu.
  80.      The menu system described here is just a starting point -- those
  81. familiar with BASIC can dress up the menu and add options.  But SHELL's
  82. modest talents do limit the menu's capabilities.  As noted earlier,
  83. certain kinds of programs cannot be executed using SHELL.  Furthermore,
  84. before a SHELL command executes, BASIC accesses COMMAND.COM in the root
  85. directory of the disk from which the system was started, which slows
  86. down operations.  With DOS 3.0 and later versions, copying COMMAND.COM
  87. to a RAM disk (E: in this example) and issuing the DOS command:  SET
  88. COMSPEC=E:\COMMAND.COM  minimizes that delay.
  89.      The second BASIC menu system eschews the SHELL command and instead
  90. relies largely on two batch files.  MENU.BAS displays the menu, accepts
  91. user input, and creates NEXT.BAT on the fly in a RAM disk.  NEXT.BAT is
  92. a small batch file that temporarily holds the latest menu selection and
  93. passes it to MENU.BAT, a master batch file that calls the appropriate
  94. routine.  For acceptable performance, BASIC, MENU.BAS, and both batch
  95. files are kept in a RAM disk (drive E:).  A 32K RAM disk is adequate on
  96. an IBM PC; compatible systems (which lack BASIC in ROM) must dedicate
  97. more memory to the task.
  98.      With this batch/BASIC menu system, fewer restrictions apply and
  99. operations are noticeably faster than with the previous method.  You
  100. can use any version of BASIC and invoke any program or command from
  101. the menu -- including memory-resident and interpreted BASIC programs.
  102.      The master batch file, MENU.BAT, can be written with a text editor
  103. or any word processing program that creates ASCII files.  MENU.BAT
  104. doesn't change unless you add or subtract entries from the menu.
  105.      MENU.BAT begins with ECHO OFF, which suppresses the display of
  106. the batch file's commands.  The next two liens clear the screen and
  107. designate C: as the default driver.  GOTO CHOICE%1 branches execution
  108. to various labels linked to a value -- in other words, a menu selection
  109. -- that replaces the DOS variable %1.
  110.      If you have only passing acquaintance with labels, branching, and
  111. replaceable parameters, keep in mind that a batch file is composed of
  112. statements that can be issued directly from the DOS command line.  If
  113. a command is followed by parameters (such as a disk drive and file
  114. name) you can represent those parameters in the batch file with the
  115. "repoaceable parameters" %1, %2, %3, and so on.  As the batch file
  116. executes, DOS replaces every occurrence of %1 with the first parameter,
  117. every %2 with the second parameter, and so on.
  118.      For example, if you invoke MENU.BAT at the command line by typing
  119. MENU 7 4 <Enter>, the 7 and the 4 replace %1 and %2, respectively,
  120. wherever they appear in MENU.BAT.  If you only enter MENU, both %1 and
  121. %2 are null and no value is passed for either replaceable parameter.
  122. In this case, GOTO CHOICE%1 is simply read as GOTO CHOICE and execution
  123. jumps to the line following the label :CHOICE.  That line -- E:BASIC
  124. E:MENU -- runs MENU.BAS, the menu program stored in the RAM disk.
  125.      You'll notice that lines 9-149 in MENU.BAS and SHELL.BAS are
  126. almost identical.  These lines set the display modes; display menu
  127. choices, the date, and the time; and assign menu choices to the CHOICE
  128. variable.  Line 150 of MENU.BAS clears the variables PARAM$2 and
  129. PARAM$3 -- which are used only if menu option 4 or 5 is chosen -- of
  130. any previous assignments.  Line 160 diverts program flow to the
  131. selected subroutines starting at lines 1000, 2000, 3000, 4000, 5000,
  132. and 6000.
  133.      If you pick 1, 2, or 3 from the menu, MENU.BAS RETURNs operations
  134. to line 170.  Line 170 OPENs (creates) the NEXT.BAT file in the RAM
  135. disk.  Line 180 defines the contents of NEXT.BAT, namely MENU, followed
  136. by a space and the number picked from the menu.  For example, if you
  137. choose menu option 2 to run WordStar, NEXT.BAT will contain the command
  138. MENU 2.  Line 190 CLOSEs (saves) NEXT.BAT, and line 200 ends the BASIC
  139. program and returns control to the seventh line in MENU.BAT.  The sole
  140. command on this line, E:NEXT, calls the newly created NEXT.BAT into
  141. play.
  142.      Since WordStar was selected, NEXT.BAT issues the command MENU 2.
  143. MENU.BAT runs, and GOTO CHOICE%1 is now interpreted as GOTO CHOICE2.
  144. Execution branches to the line after the label :CHOICE2, changes to
  145. the WordStar subdirectory (\WS), runs WordStar, and, when WordStar is
  146. exited, changes back to the root directory.  GOTO CHOICE branches
  147. execution back to the sixth line (E:BASIC E:MENU), running MENU.BAS
  148. and starting the process again.
  149.      If you select option 4, "Check a disk," line 160 of MENU.BAS
  150. branches execution to line 4000.  The menu program asks which drive to
  151. check, then passes the letter you type to DRIVE$ on line 4010.  Line
  152. 4020 defines the variable PARAM$2 as the drive letter plus a colon.
  153. Execution RETURNs to line 170, recreating NEXT.BAT.  Line 180 puts
  154. MENU 4, plus the contents of PARMA$2, into NEXT.BAT.  If you type in
  155. A as the drive, NEXT.BAT's contents would be MENU 4 A:.
  156.      At line 200, SYSTEM terminates the BASIC program, returning
  157. control to DOS.  But because MENU.BAT is active, control actually
  158. returns to the line in MENU.BAT following E:BASIC E:MENU.  MENU.BAT
  159. calls NEXT.BAT, which issues the command MENU 4 A:.  Line 4 of MENU.BAT
  160. is now read as GOTO CHOICE4.  On the line immediately below the label
  161. :CHOICE4 is CHKDSK %2.  The variable parameter %2 is replaced by A:,
  162. and MENU.BAT sends out the command CHKDSK A:.  PAUSE gives you a chance
  163. to read the results; pressing any key executes GOTO CHIOCE, and
  164. MENU.BAS is run once more.
  165.      The processes marshalled by selectint item 5 from the menu are
  166. much the same, except that a third variable, PARAM$3, joins the crew.
  167. When you select item 6, MENU.BAS defines NEXT.BAT's contents as MENU 6.
  168. :CHOICE6 is found, the message "Type E:MENU to return to the menu"
  169. appears, echo is turned on, and the batch file ends.  And when any
  170. batch file ends, DOS reasserts control.  Of course, to display the
  171. menu again, you simply type E:MENU.
  172.      To display this menu system every time a PC is turned on or
  173. restarted, add the following lines to the system's AUTOEXEC.BAT file:
  174.  
  175. COPY \DOS\BASIC.COM E:
  176. COPY \MENU\MENU.* E:
  177. E:MENU
  178.  
  179. The first two lines copy BASIC.COM, MENU.BAS, and MENU.BAT to the RAM
  180. disk drive (E:).  E:MENU runs MENU.BAT.
  181.      Throughout all this jumping to and from among MENU.BAS, MENU.BAT,
  182. and NEXT.BAT, keep in mind that although MENU.BAS displays the menu and
  183. modifies the contents of NEXT.BAT, MENU.BAT is the real workhorse.
  184. Also, when MENU.BAT calls another batch file, it loses control and
  185. cannot regain it unless called by another batch file.  So don't use
  186. this menu system to run other batch files unless then end with the
  187. command E:MENU.
  188.      It's easy to make menu selections with the PC's function keys
  189. instead of number keys.  Just use BASIC's KEY statement at the
  190. beginning of the program to redefine the function keys for the
  191. appropriate menu options.  For example, the statement KEY 1,"1"
  192. assigns the character 1 to F1.  Refer to the KEY and KEY(n) statements
  193. in the BASIC manual for further programming ideas.
  194.      There are other ways to enhance the menu.  You merely have to tap
  195. your desires, creativity, and understanding of BASIC.  With this
  196. approach to menu making, you have full access to all BASIC commands,
  197. including those that display the PC's extended ASCII character set.
  198. If you need characters from foreign alphabets, special symbols, or
  199. graphics characters, they're available.  BASICA, of course, can
  200. provide even fancier bells and whistles, notably true graphics and
  201. sound, but you will have to set aside at least an additional 10K of
  202. memory.
  203.  
  204.  
  205. 1 'SHELL.BAS: Runs a menu system with the aid of PRINT, INKEY$, and
  206. 2 'SHELL commands.  From PC World October 1986 by Lawrence J. Magid.
  207. 9 'Set up screen
  208. 10 KEY OFF:CLS:SCREEN 0,1,0,0:WIDTH 80:COLOR 1,0
  209. 19 'Print title line and menu choices
  210. 20 PRINT "BASIC Shell Menu":COLOR 7,0:PRINT:PRINT
  211. 30 PRINT "Lotus 1-2-3..........................1"
  212. 40 PRINT "WordStar.............................2"
  213. 50 PRINT "WordPerfect..........................3"
  214. 60 PRINT "Check a disk.........................4"
  215. 70 PRINT "Copy a file..........................5"
  216. 80 PRINT "Exit to DOS..........................6"
  217. 90 PRINT:LOCATE 14,24,0:PRINT "Enter choice: __";
  218. 99 'Print date and time while waiting for input
  219. 100 CHOICE=0:WHILE CHOICE <1 OR CHOICE>6
  220. 110 CHOICE=VAL(INKEY$)
  221. 120 LOCATE 1,65:PRINT DATE$
  222. 130 LOCATE 2,65:PRINT TIME$
  223. 140 WEND
  224. 149 'Print and GOSUB to choice; then redisplay menu
  225. 150 LOCATE 14,37,1:PRINT CHOICE
  226. 160 ON CHOICE GOSUB 1000,2000,3000,4000,5000,6000
  227. 170 GOTO 10
  228. 999 'Choice 1: Lotus 1-2-3
  229. 1000 SHELL "CD\LOTUS":SHELL "123":SHELL "CD\"
  230. 1010 RETURN
  231. 1999 'Choice 2: WordStar
  232. 2000 SHELL "CD\WS":SHELL "WS":SHELL "CD\"
  233. 2010 RETURN
  234. 2999 'Choice 3: WordPerfect
  235. 3000 SHELL "CD\WP":SHELL "WP":SHELL "CD\"
  236. 3010 RETURN
  237. 3999 'Choice 4: Check a disk
  238. 4000 CLS:PRINT "Enter letter name of drive to check: ";
  239. 4010 DRIVE$=INPUT$(1):PRINT DRIVE$
  240. 4020 CHK$="CHKDSK "+DRIVE$+":":SHELL CHK$
  241. 4030 LOCATE 25,1:SHELL "PAUSE"
  242. 4040 RETURN
  243. 4999 'Choice 5: Copy a file
  244. 5000 CLS:INPUT "Enter source file name: ",SOURCE$
  245. 5010 INPUT "Enter target drive and/or file name: ",TARGET$
  246. 5020 COPY$="COPY "+SOURCE$+" "+TARGET$:SHELL COPY$
  247. 5030 RETURN
  248. 5999 'Choice 6: Exit to DOS
  249. 6000 CLS:PRINT "Type EXIT to return to the menu.":PRINT
  250. 6010 SHELL
  251. 6020 RETURN
  252.  
  253. - - - - -
  254. MENU.BAT:
  255.  
  256. ECHO OFF
  257. CLS
  258. C:
  259. GOTO CHOICE%1
  260.  
  261. :CHOICE     Display menu, write and call next.bat
  262. E:BASIC E:MENU
  263. E:NEXT
  264.  
  265. :CHOICE 1   Lotus 1-2-3
  266. CD\LOTUS
  267. 123
  268. CD\
  269. GOTO CHOICE
  270.  
  271. :CHOICE2    WordStar
  272. CD\WS
  273. WS
  274. CD\
  275. GOTO CHOICE
  276.  
  277. :CHOICE3    WordPerfect
  278. CD\WP
  279. WP
  280. CD\
  281. GOTO CHOICE
  282.  
  283. :CHOICE4    Check a disk
  284. CHKDSK %2
  285. PAUSE
  286. GOTO CHOICE
  287.  
  288. :CHOICE5    Copy a file
  289. COPY %2 %3
  290. GOTO CHOICE
  291.  
  292. :CHOICE6    Exit to DOS
  293. ECHO Type E:MENU to return to the menu.
  294. EHCO ON
  295.  
  296. - - - - -
  297. 1 'MENU.BAS: Displays the menu and modifies the contents of the
  298. 2 'temporary file, NEXT.BAT. From PC World October 1986 by
  299. 3 'Lawrence J. Magid.
  300. 9 'Set up screen
  301. 10 KEY OFF:CLS:SCREEN 0,1,0,0:WIDTH 80:COLOR 1,0
  302. 19 'Print title line and menu choices
  303. 20 PRINT "BASIC/Batch File Menu":COLOR 7,0:PRINT:PRINT
  304. 30 PRINT "Lotus 1-2-3..........................1"
  305. 40 PRINT "WordStar.............................2"
  306. 50 PRINT "WordPerfect..........................3"
  307. 60 PRINT "Check a disk.........................4"
  308. 70 PRINT "Copy a file..........................5"
  309. 80 PRINT "Exit to DOS..........................6"
  310. 90 PRINT:LOCATE 14,24,0:PRINT "Enter choice: __";
  311. 99 'Print date and time while waiting for input
  312. 100 CHOICE=0:WHILE CHOICE <1 OR CHOICE>6
  313. 110 CHOICE=VAL(INKEY$)
  314. 120 LOCATE 1,65:PRINT DATE$
  315. 130 LOCATE 2,65:PRINT TIME$
  316. 140 WEND
  317. 149 'Print and GOSUB to choice; then redisplay menu
  318. 150 LOCATE 14,37,1:PRINT CHOICE:PARAM2$="":PARAM3$=""
  319. 160 ON CHOICE GOSUB 1000,2000,3000,4000,5000,6000
  320. 170 OPEN "E:NEXT.BAT" FOR OUTPUT AS #1
  321. 180 PRINT #1,"MENU";STR$(CHOICE);" ";PARAM2$;" ";PARAM3$
  322. 190 CLOSE
  323. 200 SYSTEM
  324. 999 'Choice 1: Lotus 1-2-3
  325. 1000 RETURN
  326. 1999 'Choice 2: WordStar
  327. 2000 RETURN
  328. 2999 'Choice 3: WordPerfect
  329. 3000 RETURN
  330. 3999 'Choice 4: Check a disk
  331. 4000 CLS:PRINT "Enter letter name of drive to check: ";
  332. 4010 DRIVE$=INPUT$(1):PRINT DRIVE$
  333. 4020 PARAM2$=DRIVE+":"
  334. 4030 RETURN
  335. 4999 'Choice 5: Copy a file
  336. 5000 CLS:INPUT "Enter source file name: ",SOURCE$
  337. 5010 INPUT "Enter target drive and/or file name: ",TARGET$
  338. 5020 PARAM2$=SOURCE$:PARAM3$=TARGET$
  339. 5030 RETURN
  340. 5999 'Choice 6: Exit to DOS
  341. 6000 CLS
  342. 6010 RETURN
  343.  
  344.